package com.example.design.ddd.practice.domain.orgmng.orgservice;

import com.example.design.ddd.practice.common.framework.exception.BusinessException;
import com.example.design.ddd.practice.domain.orgmng.repository.OrgRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.example.design.ddd.practice.common.util.StringUtil.isBlank;

@Component
public class OrgNameValidator {
    @Autowired
    OrgRepository orgRepository;

    public void orgNameShouldNotEmpty(String name) {
        if (isBlank(name)) {
            throw new BusinessException("组织没有名称！");
        }
    }

    // 同一个组织下的下级组织不能重名
    public void nameShouldNotDuplicatedInSameSuperior(Long tenantId, Long superiorId, String name) {
        if (orgRepository.existsBySuperiorIdAndName(tenantId, superiorId, name)) {
            throw new BusinessException("同一上级下已经有名为'" + name + "'的组织存在！");
        }
    }

}
